layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 服务点管理
     */
    var FlowStoreCity = {
        tableId: "flowStoreCityTable"
    };

    /**
     * 初始化表格的列
     */
    FlowStoreCity.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'storeCityId', hide: true, title: 'ID'},
            {field: 'storeName', sort: true, title: '服务点名称'},
            {field: 'province', sort: true, title: '省份'},
            {field: 'city', sort: true, title: '市区'},
            {field: 'address', sort: true, title: '详细地址'},
            {field: 'isDetault', sort: true, title: '是否默认',templet:function (row) {
                return row.isDetault == 0 ? "否" : "是";
            }},
            {field: 'createTime', sort: true, title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作',width:300}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FlowStoreCity.search = function () {
        var queryData = {};

        queryData['storeName'] = $('#storeName').val();

        table.reload(FlowStoreCity.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    FlowStoreCity.openAddDlg = function () {
        func.open({
            title: '添加服务点',
            content: Feng.ctxPath + '/flowStoreCity/add',
            tableId: FlowStoreCity.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      FlowStoreCity.openEditDlg = function (data) {
          func.open({
              title: '修改服务点',
              content: Feng.ctxPath + '/flowStoreCity/edit?storeCityId=' + data.storeCityId,
              tableId: FlowStoreCity.tableId
          });
      };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    FlowStoreCity.openNoteDlg = function (data) {
        func.open({
            title: '配置',
            content: Feng.ctxPath + '/flowStoreCityNote?storeCityId=' + data.storeCityId,
            tableId: FlowStoreCity.tableId
        });
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    FlowStoreCity.openGoodsDlg = function (data) {
        func.open({
            title: '物品费用配置',
            content: Feng.ctxPath + '/flowStoreCity/flowGoods?storeCityId=' + data.storeCityId,
            tableId: FlowStoreCity.tableId
        });
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    FlowStoreCity.openServiceDlg = function (data) {
        func.open({
            title: '车辆计费配置',
            content: Feng.ctxPath + '/flowStoreCity/flowServiceType?storeCityId=' + data.storeCityId,
            tableId: FlowStoreCity.tableId
        });
    };

    FlowStoreCity.openRegionDlg = function (data) {
        console.log("//////"+data);
        func.open({
            title: '市中心区域',
            content: Feng.ctxPath + '/flowStoreRegion?storeCityId=' + data.storeCityId,
            tableId: FlowStoreCity.tableId
        });
    };
    FlowStoreCity.regionPriceConfig = function (data) {
        console.log("//////"+data);
        func.open({
            title: '区到区价格',
            content: Feng.ctxPath + '/flowStoreRegionPrice?storeCityId=' + data.storeCityId,
            tableId: FlowStoreCity.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    FlowStoreCity.exportExcel = function () {
        var checkRows = table.checkStatus(FlowStoreCity.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FlowStoreCity.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/flowStoreCity/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(FlowStoreCity.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("storeCityId", data.storeCityId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FlowStoreCity.tableId,
        url: Feng.ctxPath + '/flowStoreCity/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: FlowStoreCity.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        FlowStoreCity.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    FlowStoreCity.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        FlowStoreCity.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + FlowStoreCity.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        console.log(";;;;;"+obj.event)
        if (layEvent === 'edit') {
            FlowStoreCity.openEditDlg(data);
        } else if (layEvent === 'delete') {
            FlowStoreCity.onDeleteItem(data);
        } else if (layEvent === 'noteConfig') {
            FlowStoreCity.openNoteDlg(data);
        } else if (layEvent === 'goodsConfig') {
            FlowStoreCity.openGoodsDlg(data);
        } else if (layEvent === 'serviceTypeConfig') {
            FlowStoreCity.openServiceDlg(data);
        }else if (layEvent === 'regionConfig') {
            FlowStoreCity.openRegionDlg(data);
        }else if (layEvent === 'regionPriceConfig') {
            FlowStoreCity.regionPriceConfig(data);
        }
    });
});
